package com.elephant.core.interceptor.method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * MethodInterceptor是AOP项目中的拦截器，它拦截的目标是方法，即使不是Controller中的方法。
 *
 * 实现MethodInterceptor拦截器大致也分为两种，一种是实现MethodInterceptor接口，另一种利用Aspect的注解或配置。
 * @Author: hai
 * @Date: 2019/2/12 15:10
 */
@Component
public class MethodInterceptorTest implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) {
        Object[] args = methodInvocation.getArguments();
        Method method = methodInvocation.getMethod();
        System.out.println("method : " + method.getName());
        try {
            return methodInvocation.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
